package com.applozic.mobicomkit.uiwidgets.conversation.view;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.api.conversation.Message;
import com.applozic.mobicomkit.contact.BaseContactService;
import com.applozic.mobicomkit.ChatConfig;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GetPacketResultActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import utils.Utils;

public class GetRedPacketResponseView extends BaseRelativeLayout {
    @BindView(R.id.red_packet)
    ImageView redPacket;
    @BindView(R.id.red_text)
    TextView red_text;
    @BindView(R.id.messageLayout)
    RelativeLayout messageLayout;
    private BaseContactService contactService;
    private Message message;

    public GetRedPacketResponseView(Context context) {
        super(context);
    }

    public GetRedPacketResponseView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GetRedPacketResponseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.send_red_packet_response_layout);


        messageLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!UserInfo.getToken().equals("")) {//登录才可以点进去
                    if (UserInfo.getIsTry() == 1 || UserInfo.getIsTest() != 1) {//试玩账号不能点进去
                        if (message == null) {

                        }
                        Intent intent = new Intent(mContext, GetPacketResultActivity.class);
                        intent.putExtra(ChatConfig.PACKET_GET_ID, message.getMetadata().get(ChatConfig.PACKET_GET_ID));
                        intent.putExtra(ChatConfig.PACKET_ID, message.getMetadata().get(ChatConfig.PACKET_ID));
                        mContext.startActivity(intent);
                    } else {
                        Utils.showToast(Common.IS_TEST);
                    }
                } else {
                    Utils.showToast("请先登录");
                }
            }
        });
    }

    public void setData(Message message, BaseContactService contactService) {
        this.message = message;
        this.contactService = contactService;
        if (UserInfo.getToken().equals("")) {
            red_text.setText(Html.fromHtml(contactService.getContactById(message.getMetadata().get(ChatConfig.PACKET_TO)).getDisplayName() + "领取了" + contactService.getContactById(message.getMetadata().get(ChatConfig.PACKET_FROM)).getDisplayName() + "的<font color='#661dac'>红包</font>"));
        } else {
            String get;
            String from;
            if (message.getMetadata().get(ChatConfig.PACKET_TO) != null && message.getMetadata().get(ChatConfig.PACKET_TO).equals(UserInfo.getUsername())) {
                get = "我";
            } else {
                get = contactService.getContactById(message.getMetadata().get(ChatConfig.PACKET_TO)).getDisplayName();
            }
            if (message.getMetadata().get(ChatConfig.PACKET_FROM) != null && message.getMetadata().get(ChatConfig.PACKET_FROM).equals(UserInfo.getUsername())) {
                from = "我";
            } else {
                from = contactService.getContactById(message.getMetadata().get(ChatConfig.PACKET_FROM)).getDisplayName();
            }
            red_text.setText(Html.fromHtml(get + "领取了" + from + "的<font color='#661dac'>红包</font>"));
        }
    }

    @Override
    protected void load(Context context) {

    }
}
